package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.CwzbJtys;
import com.corpcredit.model.Jbqk;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class CwzbJtysForm extends BasePage implements Serializable {
    private GenericManager<CwzbJtys, Long> cwzbJtysManager;
    private CwzbJtys cwzbJtys = new CwzbJtys();
    private Long id;
    
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }
    public void setCwzbJtysManager(GenericManager<CwzbJtys, Long> cwzbJtysManager) {
        this.cwzbJtysManager = cwzbJtysManager;
    }

    public CwzbJtys getCwzbJtys() {
        return cwzbJtys;
    }

    public void setCwzbJtys(CwzbJtys cwzbJtys) {
        this.cwzbJtys = cwzbJtys;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        cwzbJtysManager.remove(cwzbJtys.getId());
        addMessage("cwzbJtys.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            cwzbJtys = cwzbJtysManager.get(id);
        } else {
            cwzbJtys = new CwzbJtys();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (cwzbJtys.getId() == null || cwzbJtys.getId() == 0);
     // set the related jbqk as a foreign key.
        cwzbJtys.setJbqk( jbqk );
        // set username who act this operation, get the user name from httprequest
        cwzbJtys.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        cwzbJtys.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        cwzbJtys.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
       
        cwzbJtysManager.save(cwzbJtys);

        String key = (isNew) ? "cwzbJtys.added" : "cwzbJtys.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	 // set the id to the next page
            this.id = cwzbJtys.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 